# demo03_mail.py
from email.mime.text import MIMEText  # 导入邮件主体内容的类
from email.header import Header  # 导入邮件头部信息的类
import smtplib
# plain: 表示纯文本   utf8:邮件字符集
# MIMEText(邮件正文, "plain", "utf8")
msg = MIMEText("python email", "plain", "utf8")
msg["From"] = Header("dcc", "utf8")  # 发送者的头部信息
msg["To"] = Header("tcc", "utf8")  # 接收者的头部信息
msg["Subject"] = Header("test", "utf8")  # 标题的头部信息
smtp = smtplib.SMTP("127.0.0.1")  # 创建对象，声明发送邮件的服务器
# msg.as_bytes() 指定以字节的方式发送邮件
# 发邮件: sendmail(发件人, 收件人, 邮件主体)
smtp.sendmail("root", ["bob","alice"], msg.as_bytes())
# 测试:
# 代码执行前:
# [root@localhost NSD2202]# mail -u bob
# No mail for bob
# [root@localhost NSD2202]# mail -u alice
# No mail for alice
# 代码执行后:
# [root@localhost NSD2202]# mail -u bob
# Heirloom Mail version 12.5 7/5/10.  Type ? for help.
# "/var/mail/bob": 1 message 1 new
# >N  1 =?utf8?q?dcc?=@local  Tue May 31 17:50  19/651   "test"
# & Held 1 message in /var/mail/bob
# [root@localhost NSD2202]# mail -u alice
# Heirloom Mail version 12.5 7/5/10.  Type ? for help.
# "/var/mail/alice": 1 message 1 new
# >N  1 =?utf8?q?dcc?=@local  Tue May 31 17:50  19/655   "test"
# & Held 1 message in /var/mail/alice